﻿using padifscommonlib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace client
{
    public class Client : AbstractSystemComponent
    {
        public string metadataServer0Name { get; set; }
        public int metadataServer0Port { get; set; }
        public int metadataClient0Port { get; set; }
        public string metadataServer0Address { get; set; }

        public string metadataServer1Name { get; set; }
        public int metadataServer1Port { get; set; }
        public int metadataClient1Port { get; set; }
        public string metadataServer1Address { get; set; }

        public string metadataServer2Name { get; set; }
        public int metadataServer2Port { get; set; }
        public int metadataClient2Port { get; set; }
        public string metadataServer2Address { get; set; }

        public Client(string name,string address, int port):base(name,address,port)
        {
            try
            {
                BinaryServerFormatterSinkProvider bp = new BinaryServerFormatterSinkProvider();
                ClientIPServerSinkProvider csp = new ClientIPServerSinkProvider();
                csp.Next = bp;
                Hashtable ht = new Hashtable();
                ht.Add("name", name);
                ht.Add("port", port);
                TcpChannel channel = new TcpChannel(ht, null, csp);
                ChannelServices.RegisterChannel(channel, false);

                this.metadataServer0Name = "m-0";
                this.metadataServer0Address = ConfigurationManager.AppSettings["m-0-address"];
                this.metadataServer0Port = Int32.Parse(ConfigurationManager.AppSettings["m-0-server-port"]);
                this.metadataClient0Port = Int32.Parse(ConfigurationManager.AppSettings["m-0-client-port"]);

                this.metadataServer1Name = "m-1";
                this.metadataServer1Address = ConfigurationManager.AppSettings["m-1-address"];
                this.metadataServer1Port = Int32.Parse(ConfigurationManager.AppSettings["m-1-server-port"]);
                this.metadataClient1Port = Int32.Parse(ConfigurationManager.AppSettings["m-1-client-port"]);

                this.metadataServer2Name = "m-2";
                this.metadataServer2Address = ConfigurationManager.AppSettings["m-2-address"];
                this.metadataServer2Port = Int32.Parse(ConfigurationManager.AppSettings["m-2-server-port"]);
                this.metadataClient2Port = Int32.Parse(ConfigurationManager.AppSettings["m-2-client-port"]);

                RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(ClientServices),
                "ClientServices",
                WellKnownObjectMode.Singleton);

                IClientServices clientServices = (IClientServices)Activator.GetObject(typeof(IClientServices),
        "tcp://" + ipAddress + ":" + port + "/ClientServices");

                clientServices.SetName(name);
                clientServices.SetMetadaServerInfo(0, this.metadataServer0Name, this.metadataServer0Address, this.metadataServer0Port, this.metadataClient0Port);
                clientServices.SetMetadaServerInfo(1, this.metadataServer1Name, this.metadataServer1Address, this.metadataServer1Port, this.metadataClient1Port);
                clientServices.SetMetadaServerInfo(2, this.metadataServer2Name, this.metadataServer2Address, this.metadataServer2Port, this.metadataClient2Port);

                Console.WriteLine("Metadata Server Info succesfully Set.");
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }

        static void initClientServices(string[] args)
        {
            string name = args[0];
            string address = args[1];
            int port = Int32.Parse(args[2]);
            Client client = new Client(name, address, port);
            System.Console.WriteLine("Client Services {0} inicializados na porta {1}", name, port);
            System.Console.ReadLine();
        }

        static int Main(string[] args)
        {
            if (args.Length > 1)
            {
                initClientServices(args);
            }
            else
            {
                System.Console.WriteLine("Não foi possivel inicializar os servicos do client por insuficiencia de parametros ...");
                System.Console.ReadLine();
            }
            return 0;
        }
    }
}
